"""remove_alert_fingerprint_constraint_for_postgresql

Revision ID: 2a6132b443ab
Revises: 971abbbf0a2c
Create Date: 2025-03-24 14:26:11.506748

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "2a6132b443ab"
down_revision = "971abbbf0a2c"
branch_labels = None
depends_on = None


def upgrade() -> None:
    conn = op.get_bind()
    dialect = conn.dialect.name

    if dialect == "postgresql":
        constraint_exists = conn.execute(
            sa.text(
                """
                SELECT conname 
                FROM pg_constraint 
                WHERE conrelid = 'alertenrichment'::regclass 
                AND conname = 'alertenrichment_alert_fingerprint_key';
            """
            )
        ).fetchone()

        with op.batch_alter_table("alertenrichment") as batch_op:
            if constraint_exists:
                batch_op.drop_constraint(
                    "alertenrichment_alert_fingerprint_key", type_="unique"
                )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    dialect = conn.dialect.name

    if dialect == "postgresql":
        with op.batch_alter_table("alertenrichment") as batch_op:
            batch_op.create_unique_constraint(
                "alertenrichment_alert_fingerprint_key",
                ["alert_fingerprint"],
            )
    # ### end Alembic commands ###
